package com.powerbank.ordercenter.mq.consumer;

import com.powerbank.common.event.device.DeviceStatusChangedEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

/**
 * 设备状态变更事件消费者
 */
@Slf4j
@Service
@RequiredArgsConstructor
@RocketMQMessageListener(
    topic = "device-status-changed-topic",
    consumerGroup = "order-center-device-consumer-group"
)
public class DeviceStatusChangedEventConsumer implements RocketMQListener<DeviceStatusChangedEvent> {

    @Override
    public void onMessage(DeviceStatusChangedEvent event) {
        try {
            log.info("接收到设备状态变更事件: deviceId={}, status={}", 
                    event.getDeviceId(), event.getNewStatus());
            
            // 处理设备状态变更逻辑
            // 如果设备故障，需要处理相关订单
            if ("FAULT".equals(event.getNewStatus())) {
                // 处理设备故障相关订单
                log.warn("设备故障，需要处理相关订单: deviceId={}", event.getDeviceId());
            }
            
        } catch (Exception e) {
            log.error("处理设备状态变更事件失败: {}", event, e);
            throw e; // 重新抛出异常，触发重试
        }
    }
}